package com.link.base.base.report.service;

import com.link.base.base.report.dao.mybatis.mapper.PurchaseReportMapper;
import com.link.base.base.report.model.PurchaseArriveReport;
import com.link.base.base.report.model.PurchaseReport;
import com.link.base.base.system.model.Period;
import com.link.base.base.system.service.PeriodService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: Wei Xuan
 * @description: 要货计划汇总、明细报表
 * @created：2018/12/01 14:36
 * @modified By：
 */
@Service
public class PurchaseReportServiceImpl implements PurchaseReportService {

    @Resource
    private PurchaseReportMapper purchaseReportMapper;

    @Resource
    private PeriodService periodService;

    /**
     * 查询要货计划产品和销售订单产品汇总情况
     *
     * @param entity
     * @return List PurchaseReport
     * @throws Exception
     */
    @Override
    public List<PurchaseReport> queryPurchasePlanProduct(PurchaseReport entity) throws Exception {
        String periodId = entity.getAttr3();
        Period perEntity = new Period();
        perEntity.setId(Long.parseLong(periodId));
        Period record = periodService.queryById(perEntity);
        if (record == null) {
            throw new ServiceException("REPORT-008");
        }

        String startDt = record.getStartDate();
        if (StringUtils.isBlank(startDt)) {
            throw new ServiceException("REPORT-009");
        }

        String endDt = record.getEndDate();
        if (StringUtils.isBlank(endDt)) {
            throw new ServiceException("REPORT-010");
        }
        entity.setAttr1(startDt);
        entity.setAttr2(endDt);
        List<PurchaseReport> purchaseReports = purchaseReportMapper.queryPurchasePlanProductPage(entity);
        for (PurchaseReport e : purchaseReports) {
            Double achieveRatio = Double.valueOf(e.getAchieveRatio()) * 100;
            Double arriveRatio = Double.valueOf(e.getArriveRatio()) * 100;
            // 保留两位小数
            String format = "%.2f";
            e.setAchieveRatio(String.format(format, achieveRatio) + "%");
            e.setArriveRatio(String.format(format, arriveRatio) + "%");
        }
        return purchaseReports;
    }

    /**
     * 查询要货计划达成报表
     *
     * @param entity
     * @return List PurchaseReport
     * @throws Exception
     */
    @Override
    public List<PurchaseArriveReport> queryPeriodProdAmt(PurchaseArriveReport entity) throws Exception {
        List<PurchaseArriveReport> list = purchaseReportMapper.queryPeriodProdAmt(entity);
        for (PurchaseArriveReport e : list) {
            Double ratio = Double.valueOf(e.getAchieveRatio()) * 100;
            Double shipRatio = Double.valueOf(e.getShipmentRatio()) * 100;
            // 保留两位小数
            String format = "%.2f";
            e.setAchieveRatio(String.format(format, ratio) + "%");
            e.setShipmentRatio(String.format(format, shipRatio) + "%");
        }

        return list;
    }


}
